// pages/mine/balance/deal/index.js
const service = require('../../../../service/other')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cardInfo: null,
        money: '',
        isReg: false,
        minNum: 0,
        bankList: [],
        bankTextList: [],
        selectIndex: '',
    },

    bindPickerChange(e) {
        let index = e.detail.value
        this.setData({
            selectIndex: index
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    onShow() {
        service.getMinimumMoney((res => {
            this.setData({
                minNum: res.data / 100
            })
        }))

        let userId = wx.getStorageSync("userInfo").id;
        service.getMyBankList(userId, (res) => {
            if (res.code == '00000') {
                if (res.data && res.data.length > 0) {

                    let bankList = res.data || []
                    let bankTextList = []
                    bankList.forEach(item => {
                        let cardNum = item.cardId
                        cardNum = cardNum.substring(cardNum.length - 4, cardNum.length - 0)
                        item.text = `${item.cardBelongsBank}(${cardNum})`
                        bankTextList.push(item.text)
                    })
                    this.setData({
                        selectIndex: 0,
                        bankTextList: bankTextList,
                        bankList: bankList
                    })
                }
            }
        })
    },

    bindMoneyInput(e) {
        let value = e.detail.value
        let minNum = this.data.minNum

        // let reg = /^[1-9][0-9]*([.][0-9]+)?$/
        let reg = /^\+?[1-9][0-9]*$/

        console.log(1111, value, reg.test(value) && (value >= minNum))
        this.setData({
            money: value,
            isReg: reg.test(value) && (value >= minNum)
        })
    },

    submit() {
        let isReg = this.data.isReg
        let selectIndex = this.data.selectIndex
        let bankList = this.data.bankList
        if (selectIndex === '') {
            wx.showToast({
                title: '请添加银行卡',
                icon: 'none'
            })
            return
        }
        if (isReg) {
            let param = {
                customerId: wx.getStorageSync("userInfo").id,
                money: this.data.money * 100,
                cardId: bankList[selectIndex].cardId,
                cardBelongsBank: bankList[selectIndex].cardBelongsBank,
                human: bankList[selectIndex].human,
            }
            service.bindSubmit(param, (res) => {
                if (res.code == '00000') {
                    if (res.data) {
                        wx.showToast({
                            title: '提现成功',
                            mask: true,
                            success: () => {
                                wx.navigateBack({
                                    delta: 1,
                                })
                            }
                        })
                    } else {
                        wx.showToast({
                            title: '提现失败',
                            icon: 'none'
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.message,
                        icon: 'none'
                    })
                }
            })
        } else {

        }
    },

    addBankCard() {
        wx.navigateTo({
            url: '/pages/mine/bankcard/index',
        })
    }






})